/**
 * 
 */
package com.example.radioclub.tasks.http;

import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;

import com.example.radioclub.data.Constants;
import com.example.radioclub.interfaces.http.IHttpJSONDownloader;

import android.os.AsyncTask;
import android.util.Log;

/**
 * Simple Http JSON Downloader.
 * 
 * @author wizard
 *
 */
public class HttpJSONDownloader extends AsyncTask<String, Void, String>{
	private final String TAG = Constants.APP_TAG + getClass().getName();
	private IHttpJSONDownloader listener;
	
	public HttpJSONDownloader(IHttpJSONDownloader listener) {
		// TODO Auto-generated constructor stub
		this.listener = listener;
	}
	
	@Override
	protected String doInBackground(String... params) {
		// TODO Auto-generated method stub
		if(params.length == 1) {
			HttpClient httpClient = new DefaultHttpClient();
			HttpGet httpGet = new HttpGet(params[0]);
			ResponseHandler<String> responseHandler = new BasicResponseHandler();
			try {
				return httpClient.execute(httpGet, responseHandler);
			} catch (Exception e) {
				// TODO: handle exception
				Log.i(TAG, e.getMessage());
			}
			return null;
		}
		return null;
		
	}
	
	@Override
	protected void onPostExecute(String result) {
		// TODO Auto-generated method stub
		this.listener.onJSONDownloadFinished(result);
		super.onPostExecute(result);
	}
}
